﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Concurrent;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace GtsServer.Service.Index.DAL
{
    public class HashIndex<K, T>
    {
        ConcurrentDictionary<K, ConcurrentHashSet<T>> indexDic = new ConcurrentDictionary<K, ConcurrentHashSet<T>>();
        object obj = new object();
        public HashSet<T> Equals(K k)
        {
            if (!indexDic.ContainsKey(k))
                return new HashSet<T>();
            return new HashSet<T>(indexDic[k]);
        }

        public void Add(K k, T t)
        {
            if (!indexDic.ContainsKey(k))
            {
                ConcurrentHashSet<T> hashSet = new ConcurrentHashSet<T>();
                hashSet.Add(t);
                indexDic[k] = hashSet;
            }
            indexDic[k].Add(t);
        }

        public void Remove(K k, T t)
        {

            if (indexDic.ContainsKey(k))
                indexDic[k].Remove(t);
        }

        public void Update(K oldK, K k, T t)
        {

            if (indexDic.ContainsKey(oldK))
                indexDic[oldK].Remove(t);
            if (!indexDic.ContainsKey(k))
                indexDic[k] = new ConcurrentHashSet<T>();
            indexDic[k].Add(t);
        }

    }
}
